<template>
  <div @click="$router.push('/article/' + article.art_id)">
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ article.title }}</span>
          <!-- 单张图片 -->
          <img
            v-lazy="article.cover.images[0]"
            alt=""
            class="thumb"
            v-if="article.cover.type === 1"
          />
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="article.cover.type === 3">
          <img
            v-lazy="item"
            alt=""
            class="thumb"
            v-for="(item, i) in article.cover.images"
            :key="i"
          />
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <span
            >{{ article.aut_name }} &nbsp;&nbsp;{{ article.comm_count }}评论
            &nbsp;&nbsp; {{ article.pubdate | dateFormat }}</span
          >
          <!-- 关闭按钮 -->
          <van-icon name="cross" @click.stop="show = !show" />
        </div>
      </template>
    </van-cell>

    <!-- 第一层：三个选项 -->
    <van-action-sheet
      v-model="show"
      cancel-text="取消"
      :closeable="false"
      get-container="body"
    >
      <div>
        <van-cell
          :title="item.name"
          clickable
          class="center-title"
          v-for="(item, i) in actions"
          :key="i"
          @click="onActionClick(item)"
        />
      </div>
    </van-action-sheet>
    <!-- 第二层九个选项 -->
    <van-action-sheet
      v-model="show2"
      cancel-text="返回"
      :closeable="false"
      @cancel="onAction2Cancel"
      get-container="body"
    >
      <div>
        <van-cell
          :title="item.label"
          clickable
          class="center-title"
          v-for="(item, i) in reports"
          :key="i"
          @click="reportArticle(item.type)"
        />
      </div>
    </van-action-sheet>
  </div>
</template>
<script>
import reports from '../../api/reports'
import { dislikeArticleAPI, reportArticleAPI } from '../../api/homeAPI'
export default {
  computed: {
    // 文章ID的计算属性
    artId () {
      return this.article.art_id.toString()
    }
  },
  name: 'ArtItem',
  props: {
    article: {
      type: Object,
      require: true
    }
  },
  data () {
    return {
      show: false,
      show2: false, // 第二个面板的显示和隐藏
      actions: [
        { name: '不感兴趣' },
        { name: '反馈垃圾内容' },
        { name: '拉黑作者' }
      ],
      reports
    }
  },
  methods: {
    async onActionClick (item) {
      if (item.name === '不感兴趣') {
        // 调用‘不感兴趣的API’删除不敢兴趣的文章
        const { data: res } = await dislikeArticleAPI(this.artId)
        if (res.message === 'OK') {
          // TODO：炸楼的操作
          this.$emit('remove-article', this.artId)
          console.log('炸楼成功咯')
        }
        // 隐藏第一个面板
        this.show = false
      } else if (item.name === '反馈垃圾内容') {
        // 隐藏第一个面板 展示第二个面板
        this.show = false
        this.show2 = true
      } else if (item.name === '拉黑作者') {
        console.log('拉黑作者')
      }
    },
    onAction2Cancel () {
      this.show2 = false
      this.show = true
    },
    async reportArticle (type) {
      // 调用api接口
      const { data: res } = await reportArticleAPI(this.article.art_id, type)

      if (res.message === 'OK') {
        console.log('举报文章成功')
        this.$emit('remove-article', this.artId)
      }
    }

  }
}
</script>

<style lang="less" scoped>
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.thumb {
  // 矩形黄金比例：0.618
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
